const demo = [
  'index.html',
  'js',
  ['src', ['1', '2'], 'index1.js', 'index2.js'],
  'css',
  ['index.css'],
];

// 不优雅
const toHTML = (obj) => {
  let res = '';
  for (let i = 0; i < obj.length; i++) {
    res += `<li>${obj[i]}`;
    if (i + 1 < obj.length && obj[i + 1] instanceof Array) {
      res += toHTML(obj[++i]);
    }
    res += `</li>`;
  }
  return `<ul>${res}</ul>`;
};

// 优雅 (考完试完善通过deep控制格式)
const toBetterHTML = (obj, deep) => {
  let res = '';
  obj.forEach((d) => {
    if (d instanceof Array) {
      res += `${toBetterHTML(d, deep + 1)}`;
    } else {
      res += `${d === obj[0] ? '' : '</li>'}<li>${d}`;
    }
    if (d === obj.slice(-1)[0]) res += '</li>';
  });
  return `<ul>${res}</ul>`;
};

console.log(toBetterHTML(demo, 0));
